package com.example.xquicdemo

import android.app.Application
import android.content.Context
import android.widget.Toast
import androidx.multidex.MultiDex
import com.inuker.bluetooth.library.BluetoothClient
import com.inuker.bluetooth.library.Code.REQUEST_SUCCESS
import com.inuker.bluetooth.library.beacon.Beacon
import com.inuker.bluetooth.library.search.SearchRequest
import com.inuker.bluetooth.library.search.SearchResult
import com.inuker.bluetooth.library.search.response.SearchResponse
import com.inuker.bluetooth.library.utils.BluetoothLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.String
import java.util.*

class AppStart: Application() {
    lateinit var mClient:BluetoothClient
    var MAC="DC:A6:32:60:F3:A4"//树莓派蓝牙的mac地址
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }
    override fun onCreate() {
        super.onCreate()
        instance=this
        mClient = BluetoothClient(this)
        val request = SearchRequest.Builder()
            .searchBluetoothLeDevice(3000, 3) // 先扫BLE设备3次，每次3s
            .searchBluetoothClassicDevice(5000) // 再扫经典蓝牙5s
            .searchBluetoothLeDevice(2000) // 再扫BLE设备2s
            .build()
        if (!mClient.isBluetoothOpened){
            mClient.openBluetooth()
        }
        mClient.search(request, object : SearchResponse {
            override fun onSearchStarted() {}
            override fun onDeviceFounded(device: SearchResult) {
                val beacon = Beacon(device.scanRecord)
                BluetoothLog.v(
                    String.format(
                        "beacon for %s\n%s",
                        device.getAddress(),
                        beacon.toString()
                    )
                )
            }
            override fun onSearchStopped() {}
            override fun onSearchCanceled() {}
        })
        connectBle{

        }
    }
    //连接蓝牙
    fun connectBle(callback:((code:Int)->Unit)){
        mClient.connect(
            MAC
        ) { code, profile ->
            GlobalScope.launch(Dispatchers.Main) {
                callback.invoke(code)
                if (code == REQUEST_SUCCESS) {
                    Toast.makeText(instance().applicationContext,"ble连接成功",Toast.LENGTH_SHORT).show()
                }else{
                    Toast.makeText(instance().applicationContext,"ble连接失败",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
    fun write(callback:(()->Unit)){
       try{
           var serviceUUID=UUID.fromString("00001801-0000-1000-8000-00805f9b34fb")
           var characterUUID=UUID.fromString("00002a05-0000-1000-8000-00805f9b34fb")
           var bytes= byteArrayOf(1)
           mClient.write(
               MAC, serviceUUID, characterUUID, bytes
           ) { code ->
               if (code == REQUEST_SUCCESS) {

               }
               callback.invoke()
           }
       }catch (e:Exception){
           e.printStackTrace()
           callback.invoke()
       }
    }
    companion object{
        private var instance: AppStart? = null
        fun instance() = instance!!
    }
}